﻿using System;
using System.Windows.Forms;

namespace ShowCat
{
    class FadeAnimation
    {
        public delegate void Clear();
        Clear clear;
        System.Timers.Timer fadeTimer;
        Form form;
        double startValue;
        double currValue;
        double endValue = 0.0;
        int count = 0;
        double rate = 0;
        double step = 10.0;
        public FadeAnimation(Form form,int time,Clear clear)
        {
            this.form = form;
            fadeTimer = new System.Timers.Timer(step);
            fadeTimer.Elapsed += doTimerEvent;
            fadeTimer.AutoReset = true;
            fadeTimer.SynchronizingObject = this.form;
            startValue = 1.0;
            currValue = startValue;
            this.clear = clear;
            count = (int)(time / step);
            rate = (startValue - endValue) / count;
        }
        public bool Isplaying()
        {
            return fadeTimer.Enabled;
        }
        public void CancelAnimat()
        {
            fadeTimer.Stop();
            form.Opacity = startValue;
        }
        public void startAnimat()
        { 
            fadeTimer.Start();
            currValue = startValue;
        }

        private void doTimerEvent(object sender, System.Timers.ElapsedEventArgs e)
        {
            currValue -= rate;
            form.Opacity = currValue;
           
            if (form.Opacity <= endValue)
            {
                fadeTimer.Stop();
                clear();
                form.Opacity = startValue;
            }
        }
    }
}
